<?php
/**
 * @title 104. 二叉树的最大深度 - 层次遍历 - 广度优先搜索
 * @author start2004
 */

class Solution {

    /**
     * @param TreeNode $root
     * @return Integer
     */
    function maxDepth($root) {
        /**
         * @since 2020-07-28 极端情况
         */
        if($root === null){
            return 0;
        } else {}

        /**
         * @since 2020-07-28 二叉树深度
         */
        $deep = 0;

        /**
         * @since 2020-07-28 遍历二叉树
         */
        $nexts = [$root];
        while($nexts){
            $nextTmp = [];
            foreach ($nexts as $leaf){
                if($leaf->left !== null){
                    $nextTmp[] = $leaf->left;
                } else {}

                if($leaf->right !== null){
                    $nextTmp[] = $leaf->right;
                } else {}
            }

            $nexts = $nextTmp;
            $deep++;
        }

        /**
         * @return
         */
        return $deep;
    }
}





/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.28.php
 */
$datas = [
    [3,9,20,null,null,15,7],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->maxDepth($xhprof->tree($data));
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
